package com.zilu.digitaltwin.dto.resp;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.zilu.digitaltwin.entity.NumberAttribute;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: NumberAttributeRespDTO
 * @Description:
 * @Author: 段振宇
 * @Date: 2023/1/5 10:59
 */
@NoArgsConstructor
@Data
public class NumberAttributeRespDTO {

    @JsonProperty("numberName")
    private String numberName;
    @JsonProperty("numberType")
    private String numberType;
    @JsonProperty("unit")
    private String unit;
    @JsonProperty("item")
    private List<ItemDTO> item;

    @NoArgsConstructor
    @Data
    public static class ItemDTO {
        @JsonProperty("id")
        private Integer id;
        @JsonProperty("startIndex")
        private Integer startIndex;
        @JsonProperty("endIndex")
        private Integer endIndex;
        @JsonProperty("color")
        private String color;
        @JsonProperty("sectionNumber")
        private Integer sectionNumber;

        public static ItemDTO converToDto(NumberAttribute attribute){
            ItemDTO dto = new ItemDTO();
            dto.setColor(attribute.getColor());
            dto.setStartIndex(attribute.getStartIndex());
            dto.setEndIndex(attribute.getEndIndex());
            dto.setId(attribute.getId());
            return dto;
        }
    }

    public void sortNumberAttributeItem(){
        List<NumberAttributeRespDTO.ItemDTO> itemDTOS =
                this.item.stream().sorted(Comparator.comparingInt(NumberAttributeRespDTO.ItemDTO::getStartIndex).reversed())
                        .collect(Collectors.toList());
        this.setItem(itemDTOS);
    }
}
